﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using MShop.Core;
using MShop.Core.Util;
using MShop.Core.Configuration;
using MShop.Core.DataAccess;
using MShop.Core.Domain;
using MShop.Core.Service.Membership;

namespace MShop.Web.Components
{
    public class ShopContext : IShopContext
    {
        private readonly ICommonResolver _CommonResolver;
        private readonly IShopSettingsDao _ShopSettingsDao;       
        public ShopContext(ICommonResolver commonResolver, IShopSettingsDao shopSettingsDao)
        {
            _CommonResolver = commonResolver;
            _ShopSettingsDao = shopSettingsDao;          
        }

        public static IShopContext Current
        {
            get { return IoC.Resolve<IShopContext>(); }
        } 

        public ShopCfgSettings Settings
        {
            get 
            {
               return  _ShopSettingsDao.GetInstance();
            }
        }

        public string PhysicalSiteDataDirectory
        {
            get { throw new NotImplementedException(); }
        }

        public Core.Domain.aspnet_Users CurrentUser
        {
            get 
            {
                if (HttpContext.Current == null)
                    throw new ArgumentNullException();

                if (HttpContext.Current.User.Identity.IsAuthenticated)
                {
                    return _CommonResolver.GetCommon<aspnet_Users>().FirstOrDefault(x => x.UserName == HttpContext.Current.User.Identity.Name);
                }

                return null;
            }
        }

        public string Theme
        {
            get { throw new NotImplementedException(); }
        }


        public string SessionId
        {
            get 
            {
                HttpCookie cookie = HttpContext.Current.Request.Cookies["SessionId"];
                if (cookie != null)
                {
                    return cookie.Value;
                }
                return null;
            }
        }

    

       
    }
}